{{- $_ := set . "Version" ( splitList "/" .Values.global.doc_version | last ) }}

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend
  annotations:
    "werf.io/replicas-on-creation": "1"
  labels:
    service: frontend
spec:
  minReadySeconds: 5
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      service: frontend
  template:
    metadata:
      labels:
        service: frontend
    spec:
      imagePullSecrets:
        - name: deckhouse-registry-site
      priorityClassName: {{ pluck .Values.werf.env .Values.priorityClassName | first | default .Values.priorityClassName._default }}
      serviceAccountName: docs-builder
      containers:
        - name: frontend
          command: ["nginx", "-g", "daemon off;"]
          image: {{ index .Values.werf.image "web-frontend" }}
          {{- include "resources" . | nindent 10 }}
          ports:
          - containerPort: 8080
            name: http
            protocol: TCP
          lifecycle:
            preStop:
              exec:
                command: ["/usr/sbin/nginx", "-s", "quit"]
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8080
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8080
          volumeMounts:
            - mountPath: /app/en/modules
              name: modules-docs-en
            - mountPath: /app/ru/modules
              name: modules-docs-ru
            - mountPath: /cache
              name: download-cache
        - name: builder
          command:
            - "/app/server"
            - "-highAvailability={{pluck .Values.werf.env .Values.docsBuilder.highAvailability | first | default .Values.docsBuilder.highAvailability._default}}"
          image: {{ index .Values.werf.image "docs-builder" }}
          {{- include "resources" . | nindent 10 }}
          env:
            - name: HUGO_ENVIRONMENT
              value: production
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - name: CLUSTER_DOMAIN
              value: "cluster.local"
          ports:
          - containerPort: 8081
            name: http
            protocol: TCP
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8081
          readinessProbe:
            httpGet:
              path: /readyz
              port: 8081
          volumeMounts:
            - mountPath: "/mount/public/en/modules"
              name: modules-docs-en
            - mountPath: "/mount/public/ru/modules"
              name: modules-docs-ru
      volumes:
        - name: modules-docs-en
          emptyDir: {}
        - name: modules-docs-ru
          emptyDir: {}
        - name: download-cache
          emptyDir: {}

---
apiVersion: v1
kind: Service
metadata:
  name: frontend
spec:
  clusterIP: None
  selector:
    service: frontend
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: frontend
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: frontend
  updatePolicy:
    updateMode: "Auto"
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: frontend
spec:
  maxUnavailable: 1
  selector:
    matchLabels:
      service: frontend
